<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <div style="margin-top:20px; text-align:right">
      <el-button type="primary" @click="hSubmit">确认</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { processingDepartments } from '@/utils'
import { assignPerm, getRolesById } from '@/api/settings'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 存储权限数据: 'value',
    }
  },
  methods: {
    async loadPermissionList() {
    // 发送请求, 获取权限列表
      const { data } = await getPermissionList()
      console.log('权限列表的数据是', data)
      this.list = processingDepartments(data.data)
      this.setCheckedKeys()
    },
    async setCheckedKeys() {
      const res = await getRolesById(this.id)
      console.log(res.data.data.permIds)
      this.$refs.tree.setCheckedKeys(res.data.data.permIds)
    },
    // 清空
    resetChecked() {
      this.$refs.tree.setCheckedKeys([])
    },
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.id, permIds: permIds })
        console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
